/***************************************************************************
 *   Copyright (C) 2009 by betox2099   *
 *   betox2099@yahoo.com.mx   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
/**
Nota: probar utilzar la configuración de la posicion de los bloques 
mediante una matriz

Ejem:

0100
0100
0110
0000
*/
#include "Puzzle.h"

Puzzle::Puzzle(int shape[4][4],int new_pivot_x,int new_pivot_y,int new_color)
{
	position = new CL_Point();
	position->x = 0;
	position->y = 0;
	pivot = new CL_Point();
	pivot->x = new_pivot_x;
	pivot->y = new_pivot_y;
	color = new_color;
	setShape(shape);
}

Puzzle::~Puzzle()
{
	free(position);
	free(pivot);
}

void Puzzle::move(int new_x,int new_y)
{
	position->x = new_x;
	position->y = new_y;
}

void Puzzle::moveLeft()
{
	int temp_x;
	temp_x = position->x-1;
	move(temp_x,position->y);
}

void Puzzle::moveRight()
{
	int temp_x;
	temp_x = position->x+1;
	move(temp_x,position->y);
}

void Puzzle::moveDown()
{
	int temp_y;
	temp_y = position->y+1;
	move(position->x,temp_y);
}

void Puzzle::turnLeft()
{
	int i,j,temp[4][4];
	int pivot_update = 0;
	for(i=0; i<4; i++){
		for(j=0; j<4; j++){
			temp[3-i][ j ]=shape[ j ][i];
			if(!pivot_update && j == pivot->x && i == pivot->y){
				pivot->x = 3-(3-i);
				pivot->y = 3-j;
				pivot_update = 1;
				//std::cout<<"p_X:"<<pivot_x<<" p_Y:"<<pivot_y<<std::endl;
			}
		}
	}
	
	setShape(temp);
}

void Puzzle::turnRight()
{
	int i,j,temp[4][4];
	int pivot_update = 0;
	for(i=0; i<4; i++){
		for(j=0; j<4; j++){
			temp[j][ i ]=shape[3-i][j];
			if(!pivot_update &&(3-i) == pivot->x && j == pivot->y){
				pivot->x = 3-j;
				pivot->y = 3-i;
				pivot_update = 1;
				//std::cout<<"p_X:"<<pivot_x<<" p_Y:"<<pivot_y<<std::endl;
			}
		}
	}
	
	setShape(temp);
}

std::vector<CL_Point *> Puzzle::getBlockPosition()
{
	int i,j,delta_x,delta_y;
	std::vector<CL_Point *> v;
	CL_Point *p;
	delta_x = position->x-pivot->x;
	delta_y = position->y-pivot->y;
	/*std::cout<<"x:"<<x<<" y:"<<y
             <<" p_x:"<<pivot_x<<" p_y:"<<pivot_y
             <<"delta_x:"<<delta_x<<" delta_y:"<<delta_y<<std::endl;*/
	for(i=0;i<4;i++){
		for(j=0;j<4;j++){
			if(shape[j][i] > 0){
				//std::cout<<"j:"<<j<<" i:"<<i<<std::endl;
				p = new CL_Point();
				p->y = j+delta_y;
				p->x = i+delta_x;
				v.push_back(p);
			}
		}
	}

	return v;
}

std::vector<Block *> Puzzle::getBlocks()
{
	int i,j,delta_x,delta_y;
	std::vector<Block *> v;
	Block *p;
	delta_x = position->x-pivot->x;
	delta_y = position->y-pivot->y;
	/*std::cout<<"x:"<<x<<" y:"<<y
             <<" p_x:"<<pivot_x<<" p_y:"<<pivot_y
             <<"delta_x:"<<delta_x<<" delta_y:"<<delta_y<<std::endl;*/
	for(i=0;i<4;i++){
		for(j=0;j<4;j++){
			if(shape[j][i] > 0){
				//std::cout<<"j:"<<j<<" i:"<<i<<std::endl;
				p = new Block(i+delta_x,j+delta_y,shape[j][i]);
				v.push_back(p);
			}
		}
	}

	return v;
}

int Puzzle::getColor()
{	
	return color;
}

void Puzzle::setShape(int new_shape[4][4])
{
	int i,j;
	
	for(i=0; i<4; i++){
		for(j=0; j<4; j++){
			shape[j][i]= new_shape[j][i];
		}
	}
}

void Puzzle::setColor(int new_color)
{
	color = new_color;
}
